package com.share.controller;

import com.share.domain.Response;
import com.share.util.Anoymous;
import com.share.util.JwtTokenUtil;
import org.joda.time.DateTime;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/*
    数据绑定:
    一、@ResponseBody 返回中文乱码
    https://www.cnblogs.com/lvbinbin2yujie/p/10611415.html

    二、
 */
@Controller
@RequestMapping("/data")
public class C4_tokenLogin extends BaseController {

    @RequestMapping("/login")
    @ResponseBody
    @Anoymous
    public Response login(HttpServletRequest request) {

        Response response = new Response();
        //payload
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", "sunxj");
        map.put("exp", DateTime.now().plusSeconds(40).toDate().getTime()/1000);

        response.setToken(JwtTokenUtil.genToken(map));
        response.setCode("200");
        response.setMsg("success");
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Set-Cookie", "access_token = " + JwtTokenUtil.genToken(map)+ ";Path=/;HttpOnly");
        response.setHeaders(headers);
        return response;
    }

}
